/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsc.war.form;

import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.Topic;
import bsc.ejb.metadata.Colorimetry;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.model.Score;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta; castrosalazar; memo
 */
public class AddSRMTopic extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("addSRMTopic");
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idSRM = Long.parseLong(request.getParameter("srm"));

            SRM SRM = delegate.findSRM(idSRM);

            //look for suggested topics by ScoreCards
            List<ScoreCard> badScoreCards = ScoreCardUtils.getSuggestedTopics(
                    delegate, ScoreCardUtils.getSystemComparation(request), SRM.getScoreView(), ScoreCardUtils.getSystemDate(request));

            List<Topic> listTopics =  /*delegate.findTopicsView( SRM.getScoreView() );*/ delegate.findTopic( SRM.getScoreView() );
            
            List<Topic> topics = new ArrayList<Topic>();
            List<ScoreCard> scoreCars = new ArrayList<ScoreCard>();

            java.util.Map<ScoreCard, String> colors = new HashMap<ScoreCard, String>();
            java.util.Map<ScoreCard, Double> scores = new HashMap<ScoreCard, Double>();

            for (Topic topic : listTopics)
            {
                if(topic.getScoreCard() != null && badScoreCards.contains(topic.getScoreCard()))
                    scoreCars.add(topic.getScoreCard());
                if(delegate.findSRMTopic(SRM,topic) != null)
                    topics.add(topic);
            }

            for (Topic topic : topics)
                listTopics.remove(topic);
            for (ScoreCard scoreCard : scoreCars)
                badScoreCards.remove(scoreCard);

            for( ScoreCard scoreCard : badScoreCards )
            {
                Score score = ScoreCardUtils.calculateScore(
                        delegate, ScoreCardUtils.getSystemComparation(request), scoreCard, ScoreCardUtils.getSystemDate(request));
                Colorimetry colorimetry = delegate.findColorimetry(scoreCard, score.getScore());
                scores.put(scoreCard, score.getScore());
                colors.put(scoreCard, colorimetry.getColor());
            }

            request.setAttribute("scores", scores);
            request.setAttribute("colors", colors);
            request.setAttribute("badScoreCards", badScoreCards);
            request.setAttribute("listTopics", listTopics);
            request.setAttribute("srm", idSRM);

        } catch (Exception e)
        {
            logger.error("Caught exception: ", e);
        }
        return mav;
    }
}
